package com.zscat.mallplus.center.controller;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.center.entity.CenterInvoice;
import com.zscat.mallplus.center.entity.CenterInvoicesManage;
import com.zscat.mallplus.center.entity.CenterJifen;
import com.zscat.mallplus.center.service.ICenterInvoiceService;
import com.zscat.mallplus.center.service.ICenterInvoicesManageService;
import com.zscat.mallplus.oms.entity.OmsPayments;
import com.zscat.mallplus.utils.CommonResult;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/CenterInvoicesManage")
public class CenterInvoicesManageController {
	
	@Resource
	private ICenterInvoicesManageService centerInvoicesManageService;
	@Resource
    private ICenterInvoiceService centerInvoiceService;

	/**
	 * 申请发票列表
	 */
    @GetMapping("/list")
    public Object list(CenterInvoicesManage entity,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
		try {
			return new CommonResult().success(centerInvoicesManageService.page(new Page<CenterInvoicesManage>(pageNum, pageSize), new QueryWrapper<>(entity)));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return new CommonResult().failed();
	}
	
    /**
     * 申请开票
     */
    @PostMapping(value = "/create")
    public Object saveOmsPayments(@RequestBody CenterInvoicesManage entity) {
        try {
        	CenterInvoice centerInvoice = centerInvoiceService.selectCenterInvoiceById(entity.getInvoicesId());
        	entity.setGroupName(centerInvoice.getGroupName());
        	entity.setGroupCode(centerInvoice.getGroupCode());
            if (centerInvoicesManageService.save(entity)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("保存开票申请：%s", e.getMessage(), e);
            return new CommonResult().failed(e.getMessage());
        }
        return new CommonResult().failed();
    }
    
}
